package utils;

import groovy.lang.Closure;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jj.play.org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
import jj.play.org.eclipse.mylyn.wikitext.textile.core.TextileLanguage;

import org.apache.commons.lang.StringUtils;

import play.Logger;
import play.Play;
import play.cache.Cache;
import play.exceptions.TagInternalException;
import play.exceptions.TemplateExecutionException;
import play.libs.IO;
import play.mvc.Router;
import play.templates.FastTags;
import play.templates.GroovyTemplate.ExecutableTemplate;
import play.vfs.VirtualFile;

/**
 * 拓展页面模板标签
 * 
 * @author chao
 */
public class XTags extends FastTags {
	
	static int getConcatMaxFiles(){
		String maxNum = Play.configuration.getProperty("asset.max");
		try{
			return Integer.parseInt(maxNum);
		}catch(Exception e){
			return 10;
		}
	}


	/**
	 * 输出静态文件，用于避免引擎解析的文本输出
	 * 
	 * @param args
	 * @param body
	 * @param out
	 * @param template
	 * @param fromLine
	 */
	public static void _output(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
		if (!args.containsKey("arg") || args.get("arg") == null) {
			throw new TemplateExecutionException(template.template, fromLine, "Specify a file name",
					new TagInternalException("Specify a file name"));
		}
		String filePath = args.get("arg").toString();
		String filePathInProject = "app/views/" + filePath;
		try {
			String key = "OutputFile-" + filePathInProject;
			String content = Cache.get(key, String.class);
			if (content == null) {
				File file = Play.getVirtualFile(filePathInProject).getRealFile();
				content = IO.readContentAsString(file);
				if (Play.mode.isProd()) {
					Cache.set(key, content);
				}
			}
			out.write(content);
		} catch (Exception e) {
			Logger.error(e, "Cannot output %s", filePath);
		}
	}

	/**
	 * 编译输出textile文件
	 * 
	 * @param args
	 * @param body
	 * @param out
	 * @param template
	 * @param fromLine
	 */
	public static void _textile(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
		if (!args.containsKey("arg") || args.get("arg") == null) {
			throw new TemplateExecutionException(template.template, fromLine, "Specify a file name",
					new TagInternalException("Specify a file name"));
		}
		String filePath = args.get("arg").toString();
		String filePathInProject = "app/views/" + filePath;
		try {
			String key = "OutputTextileFile-" + filePathInProject;
			String content = Cache.get(key, String.class);
			if (content == null) {
				File file = Play.getVirtualFile(filePathInProject).getRealFile();
				content = toHTML(IO.readContentAsString(file));
				if (Play.mode.isProd()) {
					Cache.set(key, content);
				}
			}
			out.write(content);
		} catch (Exception e) {
			Logger.error(e, "Cannot compile %s", filePath);
		}
	}

	public static void _asTextile(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template,
			int fromLine) {
		if (!args.containsKey("arg") || args.get("arg") == null) {
			// do nothing
			return;
		}
		String textileContent = args.get("arg").toString();
		try {
			String htmlcontent = toHTML(textileContent);
			out.write(htmlcontent);
		} catch (Exception e) {
			Logger.error(e, "Cannot compile %s", textileContent);
		}
	}

	/**
	 * 将textile转换成html标签，去除<body/>标签
	 * 
	 * @param textile
	 * @return
	 */
	static String toHTML(String textile) {
		String html = new MarkupParser(new TextileLanguage()).parseToHtml(textile);
		html = html.substring(html.indexOf("<body>") + "<body>".length(), html.lastIndexOf("</body>"));
		return html;
	}

  
}
